/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * The Class HourSchedule.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class HourSchedule extends TimeSchedule {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 40L;

	/** The hour selected. */
	private final boolean hourSelected[] = new boolean[24];

	/** The start minute. */
	private int startMinute;

	/**
	 * Instantiates a new hour schedule.
	 *
	 * @param hour the hour
	 * @param startMinute the start minute
	 */
	public HourSchedule(int hour, int startMinute) {
		this.hourSelected[hour] = true;
		this.startMinute = startMinute;
	}

	/**
	 * Instantiates a new hour schedule.
	 *
	 * @param hours the hours
	 * @param startMinute the start minute
	 */
	public HourSchedule(int hours[], int startMinute) {
		for (final int hour : hours) {
			this.hourSelected[hour] = true;
		}
		this.startMinute = startMinute;
	}

	/**
	 * Sets the hour value.
	 *
	 * @param hour the hour
	 * @param value the value
	 */
	public void setHourValue(int hour, boolean value) {
		this.hourSelected[hour] = value;
	}

	/**
	 * Sets the all hours value.
	 *
	 * @param value the new all hours value
	 */
	public void setAllHoursValue(boolean value) {
		for (int i = 0; i < this.hourSelected.length; i++) {
			this.hourSelected[i] = value;
		}
	}

	@Override
	public boolean isDateIntoSchedule(Date date) {

		if (date == null) {
			return false;
		}

		final Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		if (this.hourSelected[calendar.get(Calendar.HOUR_OF_DAY)] == true) {
			if (this.getStartMinute() == calendar.get(Calendar.MINUTE)) {
				return true;
			}
		}

		return false;
	}

	@Override
	public boolean isDateIntoSchedule(Date date, Date lastDate) {

		final boolean result = this.isDateIntoSchedule(date);

		if (lastDate == null) {
			return result;
		}

		if (result == false) {
			return false;
		}

		final Calendar calendar = new GregorianCalendar();
		final Calendar lastCalendar = new GregorianCalendar();
		calendar.setTime(date);
		lastCalendar.setTime(lastDate);

		if ((lastCalendar.get(Calendar.YEAR) != calendar.get(Calendar.YEAR)) || (lastCalendar.get(Calendar.MONTH) != calendar.get(Calendar.MONTH)) || (lastCalendar.get(Calendar.DAY_OF_YEAR) != calendar.get(Calendar.DAY_OF_YEAR)) || (lastCalendar.get(Calendar.HOUR_OF_DAY) != calendar.get(Calendar.HOUR_OF_DAY))) {
			return true;
		}

		return false;
	}

	/**
	 * Gets the start minute.
	 *
	 * @return the start minute
	 */
	public int getStartMinute() {
		return this.startMinute;
	}

	/**
	 * Sets the start minute.
	 *
	 * @param startMinute the new start minute
	 */
	public void setStartMinute(int startMinute) {
		this.startMinute = startMinute;
	}
}
